﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client
{
    /// <summary>
    /// File: Client_Handler.cs
    /// Author: James Dibble 10009689, Harry Winser 10016143
    /// Last Revision: 20/03/12
    /// </summary>
    class ServerResponse
    {
        private String _strMessageCode, _strMessage, _strAdditionalMessage, _originalMessage;
        #region Constructor
        /// <summary>
        /// Decodes the Server Response message 
        /// </summary>
        /// <param name="message">Message Received</param>
        public ServerResponse(String message)
        {
            Console.WriteLine(message); // writes out receieved message
            _originalMessage = message;
            _strAdditionalMessage = "";
            _strMessage = "";
            int intCount = 0;
            foreach(String thisLine in message.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                if (intCount == 0) 
                {
                    String[] strCodes = thisLine.Split(' ');
                    if (strCodes.Length >= 3)
                    {
                        _strMessageCode = strCodes[1];// server message
                        for (int intCount2 = 2; intCount2 < strCodes.Length - 1; intCount2++) //server code
                        {
                            _strMessage += strCodes[intCount2];
                        }
                    }
                    else throw new ClientException("Server sent invalid response.", ClientException.ExceptionType.INVALIDSERVER);
                }
                else
                {
                    _strAdditionalMessage += thisLine;
                }
                intCount++;
            }
        }
        #endregion

        #region Properties
        public String Code
        {
            get { return _strMessageCode; }
        }

        public String Message
        {
            get { return _strMessage; }
        }

        public override string ToString()
        {
            return _originalMessage;
        }

        public String AdditionalMessage
        {
            get { return _strAdditionalMessage; }
        }
        #endregion
    }

}
